登录 白背景

594. 最长和谐子序列

https://leetcode-cn.com/problems/longest-harmonious-subsequence/

  • 提交时间:2021-11-20 15:33:13
  • 执行用时:52 ms, 在所有 Go 提交中击败了85.37%的用户
  • 内存消耗:7.2 MB, 在所有 Go 提交中击败了71.95%的用户
  • 通过测试用例:206 / 206
func findLHS(nums []int) (ans int) {
    numCountMap := make(map[int]int)
    for _, num := range nums {
        numCountMap[num]++
    }
    for num, count := range numCountMap {
        if count2, ok := numCountMap[num+1]; ok && count+count2 > ans {
            ans = count + count2
        }
    }
    return ans
}